package com.game.hot;

import com.game.data.DataMg;
import com.game.data.DataProxy;
import com.game.heartbeat.HeartbeatMg;
import com.game.heartbeat.HeartbeatProxy;
import com.game.id.IdMg;
import com.game.id.IdProxy;
import com.game.init.InitMg;
import com.game.init.InitProxy;
import com.game.load.LoadMg;
import com.game.load.LoadProxy;
import com.game.packet.PacketMg;
import com.game.packet.PacketProxy;
import com.game.role.RoleMg;
import com.game.role.RoleProxy;
import com.game.time.TimeMg;
import com.game.time.TimeProxy;
import com.game.util.LogUtil;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sig
 * @since 2016/9/24
 */
public class HotSwap {
    private static Map<String, Class<?>> map = new HashMap<>();

    static {
        map.put(HeartbeatMg.class.getSimpleName(), HeartbeatProxy.class);
        map.put(PacketMg.class.getSimpleName(), PacketProxy.class);
        map.put(HotTestMg.class.getSimpleName(), HotTestProxy.class);
        map.put(DataMg.class.getSimpleName(), DataProxy.class);
        map.put(IdMg.class.getSimpleName(), IdProxy.class);
        map.put(InitMg.class.getSimpleName(), InitProxy.class);
        map.put(LoadMg.class.getSimpleName(), LoadProxy.class);
        map.put(TimeMg.class.getSimpleName(), TimeProxy.class);
        map.put(RoleMg.class.getSimpleName(), RoleProxy.class);
    }

    public static boolean hotSwap(String className) {
        try {
            Class<?> clazz = map.get(className);
            Object object = clazz.newInstance();
            Method method = clazz.getMethod("reload", String.class);
            return (boolean) method.invoke(object, className);
        } catch (Exception e) {
            LogUtil.console.error("Oh no,[" + className + "] reload failed.");
            return false;
        }
    }
}
